package cn.lg.org.service.impl;

import cn.lg.basic.util.PageList;
import cn.lg.org.domain.Department;
import cn.lg.org.mapper.DepartmentMapper;
import cn.lg.org.query.DepartmentQuery;
import cn.lg.org.service.IDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 部门service实现类
 */
@Service
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class DepartmentServiceImpl implements IDepartmentService {

    @Autowired
    private DepartmentMapper departmentMapper;

    @Override
    public List<Department> queryAll() {
        return departmentMapper.loadAll();
    }

    @Override
    public Department queryById(Long id) {
        return departmentMapper.loadById(id);
    }

    @Override
    @Transactional
    public void save(Department department) {
        departmentMapper.save(department);
        this.update(department);
    }

    @Override
    @Transactional
    public void update(Department department) {
        //1.新增顶级部门  /自己id
        String dirPath ="";
        if(department.getParent() == null) { // 判断是否为顶级部门
            dirPath = ("/" + department.getId()); // 生成对应dirPath
        } else {
            // 获取父级对象
            Department parent = departmentMapper.loadById(department.getParent().getId());
            dirPath = (parent.getDirPath() + "/" + department.getId());
        }
        department.setDirPath(dirPath);
        departmentMapper.update(department);
    }

    @Override
    @Transactional
    public void remove(Long id) {
        departmentMapper.remove(id);
    }

    @Override
    public PageList<Department> queryData(DepartmentQuery departmentQuery) {
        // 先查询总数据量
        Integer count = departmentMapper.queryCount(departmentQuery);
        if (count == 0) { // 判断是否有数据
            return new PageList();
        }
        // 查询当前页数据
        List<Department> departments = departmentMapper.queryData(departmentQuery);
        return new PageList(count,departments);
    }

    @Override
    @Transactional
    public void patchRemove(List<Long> ids) {
        departmentMapper.patchRemove(ids);
    }

    @Override
    public List<Department> queryTree() {
        // 查出所有数据
        List<Department> departments = departmentMapper.loadAll();
        // 创建hashmap,用于保存id和department对象
        Map<Long,Department> allMap = new HashMap<>();
        // 遍历departments,并追加到allMap中
        for (Department dept : departments) {
            allMap.put(dept.getId(),dept);
        }
        // 筛选出第一层级部门
        List<Department> firstDept = new ArrayList<>();
        for (Department temp : departments) {
          if(temp.getParent_id() == null) {
              firstDept.add(temp);
          } else{
              // 获取父级部门对象
              Department parent = allMap.get(temp.getParent_id());
              if (parent != null){
                  // 将自己部门添加到children属性中
                  parent.getChildren().add(temp);
              }
          }
        }
        return firstDept; // 返回firstDept
    }
}
